package com.fingard.dsp.bank.directbank.hsbc01;

import java.io.File;

import com.fingard.dsp.bank.directConfig.TaskTimerSet;
import com.fingard.dsp.bank.directbank.bankInterface.IScheduleTask;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;
import com.fingard.util.ForeignBankUtil;

/**
 * @author XueJy on 2018/9/25.
 * @version 1.0
 */
public class HSBC01ScheduleTask extends HSBC01Base implements IScheduleTask {
    @Override
    public void execTask(TaskTimerSet timerSet) {
        int x = 0;
        try {
            if (timerSet.taskNameList.toLowerCase().contains("downreceivefiles")) {
                if("0".equals(this.getBankConfig().getValueAsString("channelFalg"))){
                    downloadFiles();//保险
                }else if("1".equals(this.getBankConfig().getValueAsString("channelFalg"))){
                    downloadFilesIsQJ();//企金旧解析方法
                }else{
                    String splitFlag=StringHelper.hasAnyChar(getBankConfig().getValueAsString("splitFlag"))
                            ?getBankConfig().getValueAsString("splitFlag"):"-";//内容分隔符
                    String startFlag=StringHelper.hasAnyChar(getBankConfig().getValueAsString("startFlag"))
                            ?getBankConfig().getValueAsString("startFlag"):"";//解析内容起始符号
                    String endFlag=StringHelper.hasAnyChar(getBankConfig().getValueAsString("endFlag"))
                            ?getBankConfig().getValueAsString("endFlag"):"";//解析内容终止符号
                    ForeignBankUtil foreignBankUtil=new ForeignBankUtil(timerSet,getBankConfig(),bankFront,getSbLog(),getCharset(),splitFlag,startFlag,endFlag);
                    foreignBankUtil.downloadFiles(HSBC01Base.class,"sftpserverHK");
                }
            }
        } catch (Exception ex) {
            this.WriteBankLogLn(ex);
        }
    }

    public void downloadFiles() {
        try {
            downloadFile();
        } catch (Exception e) {
             this.WriteBankLogLn(e);
        }
    }

    public void downloadFilesIsQJ(){
        try {
            synchronized (HSBC01Base.class) {
                //下载sftp文件
                downloadFileIsQJ();
                //List<String> fileList = new ArrayList<String>();
                //从银行下载的未处理文件存放地址
                String receiveFilePath = getBankConfig().getValueAsString("ReceiveFilePath")+File.separator;
                //解密之后文件的存放位置
                String decryptPath = getBankConfig().getValueAsString("DecryptPath") + File.separator;
                FileHelper.mkdir(decryptPath);
                File file = new File(receiveFilePath);
                //true 加密   false 不加密
                if("false".equals(getBankConfig().getValueAsString("transType"))) {
                    //不需要解密文件，直接将文件移动到解密后存放的文件夹里
                    for (String fileName : file.list()) {
                        //fileList.add(fileName);//统计解密成功的文件
                        renameTo(receiveFilePath+fileName,decryptPath+fileName);
                    }
                    //将解密文件夹中的文件进行解析
                    File file2 = new File(decryptPath);
                    if(file2.list().length>0){
                        for(String fileName:file2.list()){
                            analysisFile(fileName);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            WriteBankLogLn("下载MT文件异常："+e);
        }
    }
}
